import edoTypeError from "./error/edoTypeError";

export default function (edo: { proto: (name: string, value: any) => boolean; [key: string]: any }) {
	function prevent(event: Event) {
		event.preventDefault();
	}
	function ob(val: object) {
		const temp = { prevent };
		for (let s in val) {
			temp[s] = val[s];
		}
		return temp;
	}
	edo.proto('click', function (call: Function | null): boolean {
		try {
			if (call === null) {
				this.el.onclick = null;
			} else {
				this.el.onclick = (event: MouseEvent) => {
					const temp = ob({ 0: event });
					call.call(this, temp);
				};
			}
			return true;
		} catch {
			return false;
		}
	});
	edo.proto('onEventL', function (n: string, l: Function): boolean {
		if(!(l instanceof Function)){
			throw new edoTypeError(`This type should is Function, fault ${Object.prototype.toString.call(l).split(' ')[1].replace(']', '')}`);
		}
		this.el.addEventListener(n, l);
		return true;
	});
	edo.proto('offEventL', function (n: string, l: Function): boolean {
		if(!(l instanceof Function)){
			throw new edoTypeError(`This type should is Function, fault ${Object.prototype.toString.call(l).split(' ')[1].replace(']', '')}`);
		}
		this.el.removeEventListener(n, l);
		return true;
	});
}
